#!/bin/sh

if [ ! -L "$0" ]; then
	# if path is not a symlink, find relatively
	GITHUB_PATH=$(dirname "$(dirname "$(dirname "$(dirname "$0")")")")
else
	if command -v readlink >/dev/null; then
		# if readlink exists, follow the symlink and then find relatively
		SYMLINK=$(readlink -f "$0")
		GITHUB_PATH=$(dirname "$(dirname "$(dirname "$(dirname "$SYMLINK")")")")
	else
		# else use the standard install location
		GITHUB_PATH="/opt/GitHub Desktop"
	fi
fi
# check if this is a dev install or standard
if [ -f "$GITHUB_PATH/github-desktop-dev" ]; then
	BINARY_NAME="github-desktop-dev"
else
	BINARY_NAME="github-desktop"
fi

ELECTRON="$GITHUB_PATH/$BINARY_NAME"
CLI="$GITHUB_PATH/resources/app/cli.js"

case $1 in
	# if help in the first variable, return contents to shell
	*help*|*--help*)
		ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@";;
	# any other, redirect to /dev/null to detach from controlling terminal
	*)
		ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@" < /dev/null > /dev/null &;;
esac

exit $?
